<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<?php 
// Manage vacation records (Add, Delete and Modify)
require "hrfuns.php";
Valid_Client();
$iSQL = GetSQL(); 
?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<title>Vacation records</title>
<link rel="stylesheet" href="hr.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script src="hr.js"></script>
<script type="text/javascript">
<!--
function doAdd()
{
    if (confirm('确认添加吗？')) {
        frmAdd = document.forms['frmAddVa'];

        querystr = 'Action=Add'
                 + '&EmId=' + frmAdd.elements['selEmId'].value
                 + '&VaType=' + frmAdd.elements['selVaType'].value
                 + '&Date=' + frmAdd.elements['txtDate'].value
                 + '&Length=' + frmAdd.elements['txtLength'].value;

        var request = CreateHttpRequest();

        request.open('POST', "va_do.php", false);
        request.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        request.send(querystr);

        if (request.responseText.length > 0) 
            alert(request.responseText);
        else
            window.location.reload();
    }
}

function doDelete(frmId)
{
    frmDelete = document.forms[frmId];

    if (confirm('确认删除吗？')) {

        querystr = 'Action=Delete'
            + '&EmId=' + frmDelete.elements['txtEmId'].value
            + '&VaType= ' + frmDelete.elements['txtVaType'].value
            + '&Date=' + frmDelete.elements['txtDate'].value;

        var request = CreateHttpRequest();

        request.open('POST', "va_do.php", false);
        request.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        request.send(querystr);

        if (request.responseText.length > 0) 
            alert(request.responseText);
        else
            window.location.reload();

    }
}

function doModify(frmId)
{
    frmModify = document.forms[frmId];

    if (confirm('确认修改吗？')) {

        querystr = 'Action=Modify'
                 + '&EmId=' + frmModify.elements['txtEmId'].value
                 + '&VaType=' + frmModify.elements['txtVaType'].value
                 + '&Date=' + frmModify.elements['txtDate'].value
                 + '&Length=' + frmModify.elements['txtLength'].value;

        var request = CreateHttpRequest();

        request.open('POST', "va_do.php", false);
        request.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        request.send(querystr);

        if (request.responseText.length > 0) 
            alert(request.responseText);
    }
}

function doFilter()
{
    frmFilter = document.forms['frmFilter'];
    
    querystr = '?EmId=' + frmFilter.elements['selEmId'].value 
             + '&VaType=' + frmFilter.elements['selVaType'].value
             + '&Date=' + frmFilter.elements['selDate'].value
             + '&t=' + Math.random();

    window.location.search = querystr;
}

-->
</script>
</head>

<body>
<form id="frmAddVa">
<table bgcolor=#c8c8c8 cellspacing=1 cellpadding=1 align="center">
<tr>
    <td>&nbsp;员工</td>
    <td> <select id="selEmId" class="input-list">
<?php
    $lstEmInfo = GetEmInfoList($iSQL);
    foreach ($lstEmInfo as $eminfo) {
        echo "<option value=\"" . $eminfo['em_id'] . "\">" 
            . $eminfo['em_id'] . '| ' . $eminfo['em_name'] . "</option>\n";
    }
?>
    </select></td>
    <td>&nbsp;休假类型</td>
    <td><select id="selVaType" class="input-list">
<?php
    $lstVatype = GetVatypeList($iSQL);
    foreach ($lstVatype as $vatype) {
        echo "<option value=\"" . $vatype['type_id'] . "\">" 
            . $vatype['type_text'] . "</option>\n";
    }
?>
    </select></td>
    <td>&nbsp;休假时间</td>
    <td><input type="text" id="txtDate" class="input-box" maxlength=10 value="" /> </td>
    <td>&nbsp;小时数</td>
    <td><input type="text" id="txtLength" class="input-s-box" 
               maxlength=8 value="" /> </td>
    <td>
        <button type="button" onclick="doAdd()">Add</button>
        <input type="reset" value=" Clear " />
    </td>
</tr>
</table>
</form>
<br />
<div id="header" style="background-color:#FFA500;clear:both;text-align:right;">
<a href="/hr">返回</a></div>
<br/>
<form id="frmFilter">
<div id="Filter" style="width:160px;float:left;">
    <div style="height:25;width:60px;float:left;">员工</div>
    <div style="height:25;width:80px;float:left;"><select id="selEmId" class="input-list">
      <option value="all">All</option>
<?php
    foreach ($lstEmInfo as $eminfo) {
        echo "<option value=\"" . $eminfo['em_id'] . "\">" 
            . $eminfo['em_id'] . '| ' . $eminfo['em_name'] . "</option>\n";
    }
?></select></div>
    <div style="height:25;width:60px;float:left;">休假类型</div>
    <div style="height:25;width:80px;float:left;"><select id="selVaType" class="input-list">
      <option value="all">All</option>
<?php
    foreach ($lstVatype as $vatype) {
        echo "      <option value=\"" . $vatype['type_id'] . "\">" 
            . $vatype['type_text'] . "</option>\n";
    }
?></select></div>
    <div style="height:25;width:60px;float:left;">休假月份</div>
    <div style="height:25;width:80px;float:left;"><select id="selDate" class="input-list">
      <option value="all">All</option>
<?php
    $sqlstr = "select va_date from va_v";
    $monthes = array();
    if ($resVaDate = $iSQL->query($sqlstr)) {
        while ($row = $resVaDate->fetch_assoc()) {
            $date = $row["va_date"];
            $monthstr = substr($date, 0, 7); // yyyy-mm
            $monthes[$monthstr] = 1;
        }
    }
    ksort ($monthes);
    foreach ($monthes as $month => $value) {
        echo "      <option value=\"" . $month . "\">" . $month . "</option>";
    }
?>
    </select></div>
    <div style="height:25;width:160px;float:left;text-align:center;"><button type="button" onclick="doFilter()">Filter</button></div>
</div>
</form>

<div id="Vacations" style="width:700px;float:left;clear:right;">
<table bgcolor=#c8c8c8 cellspacing=1 cellpadding=1 align="center">
<tr>
    <th width=100>员工号</th>
    <th width=100>员工姓名</th>
    <th width=100>休假类型</th>
    <th width=100>休假时间</th>
    <th width=60>小时数</th>
    <th width=160>&nbsp;</th>
</tr>
<?php
$sqlstr = "select em_id, em_name, va_type_id, va_type, va_date, va_length"
        . " from va_v";

$haswhere = false;
if (isset($_GET["EmId"]) && $_GET["EmId"] != "all") {
    $sqlstr .= " where em_id = '" . $_GET["EmId"] . "'";
    $haswhere = true;
}

if (isset($_GET["VaType"]) && $_GET["VaType"] != "all") {
    if ($haswhere)
        $sqlstr .= " end va_type_id = " . $_GET["VaType"];
    else {
        $sqlstr .= " where va_type_id = " . $_GET["VaType"];
        $haswhere = true;
    }
}

if (isset($_GET["Date"]) && $_GET["Date"] != "all") {
    if ($haswhere)
        $sqlstr .= " and va_date like '" . $_GET["Date"] . "%'";
    else {
        $sqlstr .= " where va_date like '" . $_GET["Date"] . "%'";
        $haswhere = true;
    }
}

if ($resEm = $iSQL->query($sqlstr)) {

    $index = 0;
    while ($row = $resEm->fetch_assoc()) {
        echo "<form id=\"frm" . $index . "\">\n";
        echo "<tr>\n";
        echo "    <td><input type=\"text\" id=\"txtEmId\" readonly=\"readonly\""
            . " class=\"input-box\" value=\"" . $row["em_id"] . "\"/></td>\n";
        echo "    <td>" . $row["em_name"] . "</td>\n";
        echo "    <td><input type=\"hidden\" id=\"txtVaType\""
            . " value=\"". $row["va_type_id"] . "\" />" .$row["va_type"] ."</td>\n";
        echo "    <td><input type=\"text\" id=\"txtDate\" class=\"input-box\""
            . " readonly=\"readonly\" value=\"" . $row["va_date"] . "\"/></td>\n";
        echo "    <td><input type=\"text\" id=\"txtLength\" class=\"input-s-box\""
            . " maxlength=8 value=\"" . $row["va_length"] . "\"/></td>\n";
        echo "    <td><button type=\"button\""
            . " onclick=\"doDelete('frm" . $index . "')\">Delete</button>\n";
        echo "        <button type=\"button\""
            . " onclick=\"doModify('frm" . $index . "')\">Modify</button>\n";
        echo "    </td>\n";

        echo "</tr>\n";
        echo "</form>\n";
        $index++;
    }
}
?>
</table>
</div>
<div style="clear:both;">&nbsp;</div>
<div id="footer" style="background-color:#FFA500;clear:both;text-align:right;">
<a href="/hr">返回</a></div>

<?php
// show current employee information
$iSQL->close();
?>
</body>
</html>
